<?php

	$TEMP->set_file(array('handle' => DIR_IFRAME_VIEW . 'newAccount.html'));
	
	$accountUserID = $_GET['id'];
	$warnuserpass	= "<strong>Note:</strong> If you don't want to change your password leave the password fields blank.";
	
	$departmentList = $DSC->getListDepartment();
	
	if ( isset($_POST['subButton']) ) {
		
		$if_error		= 0;
		$if_display		= 0;
		$txtaccess		= _POST('txtaccess');
		$txtaccountno	= _POST('txtaccountno');
		$txtdepartment	= _POST('txtdepartment');
		$txtfname		= _POST('txtfname');
		$txtmname		= _POST('txtmname'); 
		$txtlname		= _POST('txtlname');
		$txtgender		= _POST('txtgender');
		$txtbdatemm		= _POST('txtbdatemm');	
		$txtbdatedd		= _POST('txtbdatedd'); 
		$txtbdateyy		= _POST('txtbdateyy');
			$txtdate	= date("Y-m-d", strtotime($txtbdateyy . '-' .$txtbdatemm . '-' . $txtbdatedd));
		$txtemail		= _POST('txtemail');
		$txtcontact		= _POST('txtcontact');
		$txtaddress		= _POST('txtaddress');
		$txtguardian	= _POST('txtguardian');
		
		$txtusername	= _POST('txtusername');
		$txtpassword	= _POST('txtpassword', false);
		
		if ( empty($txtaccountno) )	{ $if_error++; $_txtaccountno 	= WARNING_INPUT; }
		else {
			$accountNumber = $PI->accountNumber($txtaccountno);
			if ( $accountNumber > 0) {
				$if_error++; $_txtaccountno = WARNING_INPUT;
			}
		}
		if ( empty($txtfname) )		{ $if_error++; $_txtfname 		= WARNING_INPUT; }
		if ( empty($txtlname) )		{ $if_error++; $_txtlname 		= WARNING_INPUT; }
		
		if ( !empty($txtbdatemm) || !empty($txtbdatedd) || !empty($txtbdateyy) ) {
			if ( $txtdate == '1970-01-01' || $txtdate == '0000-00-00')	{ $if_error++; $_txtdate 	= WARNING_INPUT; }
		}
		else {
			$txtdate == '0000-00-00';
		}
		if (!empty($txtemail)) {
			$emailReturn = $EMAIL->validateEmail($txtemail);
			if ($emailReturn === TRUE) {
				 $if_error++;
				$_txtemail 	= WARNING_INPUT;
			}
		}
		
		if ( empty($accountUserID) ) {
			if ( empty($txtpassword) )	{ $if_error++; $_txtpassword 	= WARNING_INPUT; }
		}
		
		if ( !empty($txtusername) && !empty($txtpassword) ) {
			$accountUserPass = $PI->accountUserPass($txtusername, $txtpassword);
			if ( $accountUserPass > 0) {
				$if_error++; $_txtusername = WARNING_INPUT; $_txtpassword = WARNING_INPUT;
			}
		}
		
		
		if ( $if_error == 0 ) {
			
			$strArray = array('access'		=> $txtaccess,
							  'account_no' 	=> upperCase($txtaccountno),
							  'first_name' 	=> firstUpperCase($txtfname),
							  'last_name' 	=> firstUpperCase($txtlname),
							  'middle_name' => firstUpperCase($txtmname),
							  'birthdate' 	=> ($txtdate == '1970-01-01') ? '0000-00-00' : $txtdate,
							  'gender' 		=> $txtgender,
							  'email' 		=> $txtemail,
							  'contact_no' 	=> $txtcontact,
							  'address' 	=> firstUpperCase($txtaddress),
							  'guardian' 	=> firstUpperCase($txtguardian),
							  'dept_id' 	=> ($txtaccess == 'D' || $txtaccess == 'P') ? $txtdepartment : 0,
							  'log_user' 	=> lowerCase($txtusername),
							  'log_pass' 	=> $txtpassword);
							  
			if ( !empty($accountUserID) ) {
				$PI->updatePersonalInformation($strArray, $accountUserID);
				$warning_userpass = $warnuserpass;
			}
			else {
				$accountUserID = $PI->insertPersonalInformation($strArray);
			}
			$saved_record = "Saved";
			
			
		}
		else {
			if ( !empty($accountUserID) ) {
				$warning_userpass = $warnuserpass;
			}
			$TEMP->set_var(array('w_txtaccountno' 	=> $_txtaccountno,
								 'w_txtfname' 		=> $_txtfname,
								 'w_txtlname' 		=> $_txtlname,
								 'w_txtbdatemm'		=> $_txtdate,
								 'w_txtbdatedd'		=> $_txtdate,
								 'w_txtbdateyy'		=> $_txtdate,
								 'w_txtemail'		=> $_txtemail,
								 'w_txtusername'	=> $_txtusername,
								 'w_txtpassword'	=> $_txtpassword,
								 ));
		}
	}
	else {
		
		if ( !empty($accountUserID) ) {
			$personalInfo = $PI->getPersonalInformation($accountUserID);
			
			$txtaccountno 	= $personalInfo['account_no'];
			$txtaccess 		= $personalInfo['account_type'];
			
			$txtfname 		= $personalInfo['first_name'];
			$txtlname 		= $personalInfo['last_name'];
			$txtmname 		= $personalInfo['middle_name'];
			
			$birthdate 		= $personalInfo['birthdate'];
				$txtbdatemm	= m($birthdate);	
				$txtbdatedd	= d($birthdate); 
				$txtbdateyy	= y($birthdate);
			
			$txtgender 		= $personalInfo['gender'];
			$txtemail 		= $personalInfo['email'];
			$txtcontact 	= $personalInfo['contact_no'];
			$txtaddress 	= $personalInfo['address'];
			$txtguardian 	= $personalInfo['guardian'];
			$txtusername 	= $personalInfo['log_user'];
			$txtdepartment 	= $personalInfo['dept_idFK'];
			
			$warning_userpass = $warnuserpass;
		}
	}
	
	$selectDepartmentList = '<select name="txtdepartment" id="txtdepartment" class="select-auto-input">';
		foreach($departmentList as $departmentListVal) {
			$selectedDept = ( $txtdepartment == $departmentListVal['dept_idPK'] ) ? 'selected' : '';
			$selectDepartmentList .= '<option value="' . $departmentListVal['dept_idPK'] . '" ' . $selectedDept . '>' . $departmentListVal['dept_desc'] . '</option>';
		}
	$selectDepartmentList .= '</select>';
	
	$TEMP->set_var(array('txtaccountno' 		=> $txtaccountno,
						 'selected'.$txtaccess 	=> 'selected',
						 'txtfname' 			=> $txtfname,
						 'txtmname' 			=> $txtmname,
						 'txtlname' 			=> $txtlname,
						 'selected'.$txtgender 	=> 'selected',
						 'selected'.$txtbdatemm	=> 'selected',
						 'txtbdatedd'			=> $txtbdatedd,
						 'txtbdateyy'			=> $txtbdateyy,
						 'txtemail'				=> $txtemail,
						 'txtcontact'			=> $txtcontact,
						 'txtaddress'			=> $txtaddress,
						 'txtguardian'			=> $txtguardian,
						 'txtusername'			=> $txtusername,
						 'saved_record'			=> $saved_record,
						 'udpate_user_pass'		=> $warning_userpass,
						 'department_list'		=> $selectDepartmentList,
						 'show_dept'			=> $txtdepartment,
						 'm'					=> date("m"),
						 'd'					=> date("d"),
						 'y'					=> date("y"),
						 ));
?>